layui.use(['table', 'admin', 'ax', 'form'], function () {
    var $ = layui.$;
    var table = layui.table;
    var $ax = layui.ax;
    var admin = layui.admin;
    var form = layui.form;

    /**
     * 承运商档案管理
     */
    var BasicCarrier = {
        tableId: "basicCarrierTable"
    };

    /**
     * 初始化表格的列
     */
    BasicCarrier.initColumn = function () {
        return [[
            {type: 'checkbox'},
            {field: 'id', hide: true, title: 'Primary Key ID'},
            {field: 'operateName', sort: true, title: 'Project'},
            {field: 'carrierNumber', sort: true, title: 'Company Code'},
            {field: 'carrierName', sort: true, title: 'Company Name'},
            {field: 'v1', sort: true, title: 'Status', templet: '#v1Tpl'},
            {field: 'types', sort: true, title: 'Type'},
            {field: 'abbreviation', sort: true, title: 'Abbreviation'},
            {field: 'city', sort: true, title: 'City'},
            {field: 'xukezheng', sort: true, title: 'License Number'},
            {field: 'business', sort: true, title: 'Business Scope'},
            {field: 'legal', sort: true, title: 'Legal Representative'},
            {field: 'contacts', sort: true, title: 'Contact Person'},
            {field: 'tel', sort: true, title: 'Contact Number'},
            {field: 'xukezhengfazhengriqi', sort: true, title: 'License Issue Date'},
            {field: 'xukezhengqishiriqi', sort: true, title: 'Start Date'},
            {field: 'xukezhengzhongzhiriqi', sort: true, title: 'End Date'},
            {field: 'xukezhengfazhengjiguan', sort: true, title: 'Issuing Authority'},
            {field: 'credit', sort: true, title: 'Credit Rating'},
            {field: 'address', sort: true, title: 'Detailed Address'},

            {field: 'payee', sort: true, title: 'Settlement Party'},
            {field: 'bank', sort: true, title: 'Bank Name'},
            {field: 'bankAddress', sort: true, title: 'Bank Location'},
            {field: 'bankAccount', sort: true, title: 'Bank Account'},
            {field: 'bankNo', sort: true, title: 'Bank Code'},
            {field: 'cycle', sort: true, title: 'Settlement Cycle'},
            {field: 'taxRate', sort: true, title: 'Tax Rate'},
            {field: 'oilCardNumber', sort: true, title: 'Fuel Card Number'},

            {field: 'notes', sort: true, title: 'Notes'},
            {field: 'createTime', sort: true, title: 'Creation Time'},
            {field: 'updateTime', sort: true, title: 'Update Time'},
            {field: 'createUser', sort: true, title: 'Created By'},
            {field: 'updateUser', sort: true, title: 'Updated By'},
            {align: 'center', toolbar: '#tableBar', title: 'Operations', width:150, fixed: 'right'}
        ]];
    };

    /**
     * 点击查询按钮
     */
    BasicCarrier.search = function () {
        var queryData = {};
        queryData['operateId'] = $("#operateId").val();
        queryData['condition'] = $("#condition").val();
        table.reload(BasicCarrier.tableId, {
            where: queryData, page: {curr: 1},
             done:function () {
            }
        });
    };

    /**
     * 弹出添加对话框
     */
    BasicCarrier.openAddDlg = function () {
        window.location.href = Feng.ctxPath + '/basicCarrier/add';
    };

    /**
     * 导出excel按钮
     */
    BasicCarrier.exportExcel = function () {
        var checkRows = table.checkStatus(BasicCarrier.tableId);
        if (checkRows.data.length === 0) {
            Feng.error("Please select data to export");
        } else {
            table.exportFile(tableResult.config.id, checkRows.data, 'xls');
        }
    };

    /**
     * 点击编辑
     *
     * @param data 点击按钮时候的行数据
     */
    BasicCarrier.openEditDlg = function (data) {
        window.location.href = Feng.ctxPath + '/basicCarrier/edit?id=' + data.id;
    };

    /**
     * 点击删除
     *
     * @param data 点击按钮时候的行数据
     */
    BasicCarrier.onDeleteItem = function (data) {
        var operation = function () {
            var ajax = new $ax(Feng.ctxPath + "/basicCarrier/delete", function (data) {
                Feng.success("Deletion successful!");
                table.reload(BasicCarrier.tableId);
            }, function (data) {
                Feng.error("Deletion failed!" + data.responseJSON.message + "!");
            });
            ajax.set("id", data.id);
            ajax.start();
        };
        Feng.confirm("Are you sure to delete?", operation);
    };

    // 渲染表格
    var tableResult = table.render({
        elem: '#' + BasicCarrier.tableId,
        url: Feng.ctxPath + '/basicCarrier/list',
        page: true,
        limit: 40,
        height: "full-60",
        toolbar: '#toolbarfrom',
        cellMinWidth: 100,
        cols: BasicCarrier.initColumn()
    });

    // 搜索按钮点击事件
    $('#btnSearch').click(function () {
        BasicCarrier.search();
    });

    // 添加按钮点击事件
    $('#btnAdd').click(function () {
        BasicCarrier.openAddDlg();
    });

    // 导出excel
    $('#btnExp').click(function () {
        BasicCarrier.exportExcel();
    });

    // 工具条点击事件
    table.on('tool(' + BasicCarrier.tableId + ')', function (obj) {
        var data = obj.data;
        var layEvent = obj.event;

        if (layEvent === 'edit') {
            BasicCarrier.openEditDlg(data);
        } else if (layEvent === 'delete') {
            BasicCarrier.onDeleteItem(data);
        }
    });

    // 修改参数状态
    form.on('switch(v1)', function (obj) {
        var id = obj.elem.value;
        var checked = obj.elem.checked ? true : false;

        BasicCarrier.changeGradingStatus(id, checked);
    });

    /**
     * 修改参数状态
     *
     * @param id 参数id
     * @param checked 是否选中（true,false），选中就是启用用户，未选中就是禁用用户
     */
    BasicCarrier.changeGradingStatus = function (id, checked) {
        if (checked) {
            var ajax = new $ax(Feng.ctxPath + "/basicCarrier/disable", function (data) {
                Feng.success("Activation successful!");
            }, function (data) {
                Feng.error("Activation failed!");
                table.reload(BasicLine.tableId);
            });
            ajax.set("v1", 1);
            ajax.set("id", id);
            ajax.start();
        } else {
            var ajax = new $ax(Feng.ctxPath + "/basicCarrier/disable", function (data) {
                Feng.success("Deactivation successful!");
            }, function (data) {
                Feng.error("Deactivation failed!" + data.responseJSON.message + "!");
                table.reload(BasicLine.tableId);
            });
            ajax.set("v1", 2);
            ajax.set("id", id);
            ajax.start();
        }
    };
});
